﻿<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="login.aspx.cs" Inherits="login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Chapter 7 - Recipe 6: Using complex data types with AJAX</title>
    <script src="js/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="js/json2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("#btnSubmit").click(function(e) {
            e.preventDefault();
                
                if (($("#txtUserName").val() == "") | ($("#txtPassword").val() == ""))
                   alert("Please enter your login details");
                else
                   CheckLogin();   
            });
            
            function CheckLogin(){ 
                var LoginObj = new Object();
                LoginObj.UserName = $("#txtUserName").val();
                LoginObj.Password = $("#txtPassword").val();

                //Define DTO i.e. Data Transfer Object as follows
                var DTO = { 'userLogin': LoginObj };

                $.ajax({
                    type: "POST",
                    url: "Login.asmx/CheckLogin",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(DTO),
                    success: function(msg) {
                        if (msg.d)
                            $(".alertmsg").html("Valid Login");
                        else
                            $(".alertmsg").html("Invalid Login");
                    },
                    error: function(xhr, status, error) {
                        alert("An error has occurred during processing: " + error);
                    }
                });
           }
           
            $("#btnReset").click(function(e) {
                e.preventDefault();
                $("#txtUserName").val("");
                $("#txtPassword").val("");
                $(".alertmsg").html("");
            });
        });
    </script>
    <style media="screen" type="text/css">
    .header
    {
    	background-color:Gray;
    	font-weight:bold;
    	color:White;
    	text-align:center;
    }
    .alertmsg
    {
    	color:#FF0000;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <fieldset style="width:250px;height:130px;">
    <table border="0" cellpadding="3" cellspacing="3">
       <tr><td colspan="2" class="header">LOGIN USER</td></tr>
        <tr>
            <td align="right">
                <asp:Label ID="lblUserName" runat="server" Text="UserName: "></asp:Label>
            </td>
            <td align="left">
                <asp:TextBox ID="txtUserName" runat="server" Width="150px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="lblPassword" runat="server" Text="Password: "></asp:Label>
            </td>
            <td align="left">
                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="150px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit"/>&nbsp;
                <asp:Button ID="btnReset" runat="server" Text="Reset" />
            </td>
        </tr>
    </table>
    </fieldset>
        <br />
    <div align="center" class="alertmsg">
    </div>
    
</div>
    </form>
</body>
</html>

